$(function () {

    // 配置路径
    var pageUrl = config.urlAnaConfig.testPath;
    // 获取Cookie
    function getCookie(name){
        var strcookie = document.cookie;//获取cookie字符串
        var arrcookie = strcookie.split("; ");//分割
    //       console.log(arrcookie);
        //遍历匹配
        for ( var i = 0; i < arrcookie.length; i++) {
            var arr = arrcookie[i].split("=");
                if (arr[0] ==name){
                    return arr[1];
                }
        }
        return "";
    }
    var name = "tokenval";
    // tiken值
    var cookie_value = getCookie(name);
    console.log(cookie_value);
    var name1 = "userName";
    // 用户名值
    var cookie_value1 = getCookie(name1);
    $(".userName").html(cookie_value1);
    $(".nav-usename").html(cookie_value1);
    // 默认数据
    var mrData1 = "2018Q1";
    var mrData2 ="天猫";
    // 下拉数据
    // $('#usertype').selectpicker({
    //          noneSelectedText : '请选择',
    //      });

// $(".public-select-chose input").on("click",function () {
 
//     $(this).parents(".public-select-chose").siblings().find(".more-chose").hide();
//     $(".select-data2").hide();
//     $(".select-data3").hide();
//     var myThis0 = $(this);
//     var opt = $(this).attr("opt");
//     if (opt==0){
//         $(this).attr('opt','1');
//         $(this).next(".more-chose").css("display","inline-flex");
//         $(this).nextAll("span").addClass("on-off-select1");
//         $(this).nextAll("span").removeClass("on-off-select2");
//         if($(this).parent(".public-select-chose").siblings().find("input").attr("opt")==1){
//             $(this).parent(".public-select-chose").siblings().find("input").attr('opt','0');
//             $(this).parent(".public-select-chose").siblings().find("input").nextAll("span").removeClass("on-off-select1");
//             $(this).parent(".public-select-chose").siblings().find("input").nextAll("span").addClass("on-off-select2");
//         }
       
//     }else if(opt==1){
//         $(this).attr('opt','0');
//         $(this).next(".more-chose").css("display","none");
//         $(this).nextAll("span").removeClass("on-off-select1");
//         $(this).nextAll("span").addClass("on-off-select2");
//     }



// })






// $(".select-data1").on("mouseenter","li",function (){
//     var storeNameData = $(this).html();
//     var myThis = $(this);
//     $(this).siblings().removeClass("cur");
//     $(this).addClass("cur");
//     $(this).parent().next(".select-data2").show();
//     $(this).parent().nextAll(".select-data3").hide();

   
// });

// $(".select-data2").on("click","li",function (){
       
//        var b=$(this).html();
//        var h = $("#kg").val();
//         if($("#kg").val() != ""){
//             $("#kg").val(h + "，" + b); 
//         }else{
//             $("#kg").val(b);
//         }

// })

// $(document).click(function(){
//     $(".public-select-chose").find(".more-chose").hide();
//     $(".public-select-chose input").attr('opt','0');
//     $(".public-select-chose").find("span").removeClass("on-off-select1");
//     $(".public-select-chose").find("span").addClass("on-off-select2");
// });
// $(".public-select-chose").click(function(event){
//     event.stopPropagation();
// });






    $.ajax({
        url: pageUrl+'/api/topinfo/getplatforms',
        type: 'POST',
        dataType: 'json',
        headers: {
            Authorization: 'Bearer '+cookie_value
        },
        success: function (data) {
            var msg = '';
            if (data) {
                if (data.code === 200) {
//                  时间
                    $(".det_det li").remove();
                    var platforms = data.data.platforms;
                    
                    //console.log(yearsName)
                    var platformsF = $(".det_det");
                   
                    /*for in 循环*/
                     for(var i in platforms){
                         var text=`<li>${platforms[i]}</li>`;
                         platformsF.append(text);
                     };

                }
            }
        },
        error: function () {
            alert("数据错误");
            // window.location.href="login.html";
        }

    });


    $('.det_det').on("click","li",function (){
        var chooseTime = $('.details1').val();
        $('.details2').val('').attr('disabled',false).removeClass('border-hui');
        $(".det_det2 li").remove();
        // $('.details3').val('');
        $(".det_det3 li").remove()
        $('.details3').val('').attr('disabled',true).addClass('border-hui');
        $(".application").removeClass('querenguo').attr("disabled",true);
                $.ajax({
                    url: pageUrl+'/api/topinfo/getstorebyplatform',
                    type: 'POST',
                    dataType: 'json',
                    data:{
                        platform:chooseTime
                    },
                    headers: {
                        Authorization: 'Bearer '+cookie_value
                    },
                    success: function (data) {
                        var msg = '';
                        if (data) {
                            if (data.code === 200) {
                                        var stores = data.data.stores
                                        var storesF = $(".det_det2");
                                        for(var i in stores){
                                        var ss="<li>"+stores[i]+"</li>";
                                        storesF.append(ss)
                                        };

                            }
                        }
                    },
                    error: function () {
                        alert("数据错误");
                        // window.location.href="login.html";
                    }

                });



    })


    
    $('.det_det2').on("click","li",function (){
        var chooseTime = $('.details1').val();
        var chooseTime2 = $('.details2').val();
        $('.details3').val('').attr('disabled',false).removeClass('border-hui');
        $(".det_det3 li").remove()
        $(".application").removeClass('querenguo').attr("disabled",true);
        $(".det_det2 li").remove()
                $.ajax({
                    url: pageUrl+'/api/topinfo/getyearsbyplatformandstore',
                    type: 'POST',
                    dataType: 'json',
                    data:{
                        platform:chooseTime,
                        store:chooseTime2,

                    },
                    headers: {
                        Authorization: 'Bearer '+cookie_value
                    },
                    success: function (data) {
                        var msg = '';
                        if (data) {
                            if (data.code === 200) {
                                        var years = data.data.years
                                        var yearsF = $(".det_det3");
                                        for(var i in years){
                                        var ss="<li>"+years[i]+"</li>";
                                        yearsF.append(ss)
                                        };

                            }
                        }
                    },
                    error: function () {
                        alert("数据错误");
                        // window.location.href="login.html";
                    }

                });



    })
    $('.det_det3').on("click","li",function (){
            $(".application").addClass('querenguo').attr("disabled",false);
            
    })
    $.ajax({
        url: pageUrl+'/api/common/getaccesstoken',
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            var msg = '';
            if (data) {
                var accessToken = data.data.accessToken.value;
                console.log(accessToken);
                //alert(11)
                // var accessToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IkZDQmFtTFdha2tLWVhTMFFEdDJYcVFha2JPRSIsImtpZCI6IkZDQmFtTFdha2tLWVhTMFFEdDJYcVFha2JPRSJ9.eyJhdWQiOiJodHRwczovL2FuYWx5c2lzLmNoaW5hY2xvdWRhcGkuY24vcG93ZXJiaS9hcGkiLCJpc3MiOiJodHRwczovL3N0cy5jaGluYWNsb3VkYXBpLmNuLzlhZWJiZmVkLTc1MmMtNDVhYy1iZDhiLTg2ODczODZiYjg0ZS8iLCJpYXQiOjE1NTEyNTkwNTgsIm5iZiI6MTU1MTI1OTA1OCwiZXhwIjoxNTUxMjYyOTU4LCJhY2N0IjowLCJhY3IiOiIxIiwiYWlvIjoiQVNRQTIvOENBQUFBVEI1b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Y2hpbmEuY24iLCJ1cG4iOiJ6aWthaUBzYXB6ei5wYXJ0bmVyLm9ubXNjaGluYS5jbiIsInV0aSI6ImRMTENOSkJNNTBPd3dxQlZtSkVtQUEiLCJ2ZXIiOiIxLjAiLCJ3aWRzIjpbIjYyZTkwMzk0LTY5ZjUtNDIzNy05MTkwLTAxMjE3NzE0NWUxMCJdfQ.U7hDVy8gGDVBe3Ynu8-CQDWBL-JK3DWXEuHY_0vbgPDvbZk6yE1ES1VGI1wE00VLp5xBHahof4GVXJlhnoJWGSu6ZVDPu6_wDODSneTOXff58JXKa5oVvN5lf7ZA_vgMkpiWBPdpK4w8GQkGgRUvJvlUGy7e9Lh11JfDwbKVPUvtyZ03PDPPRzkUErOyBfIJy4Ups4jsN8EwYYMgcJ3lkQRU2xmPHAKOw5QSEJj6k3WEjmYhmeQNHpRFxxPF2PTaoDKZ4EM5sqdyF691k4MtHowhL68uuEtU7NgpDgGPwP-RU925nk6Nf6fNFsnn50jxvH_lYCKHd8PrRBRzNc8VGg";
                // Read embed application token from Model
                // Read embed URL from Model
                var embedUrl = "https://app.powerbi.cn/reportEmbed?reportId=af34d2e5-3f9d-4e88-ae1d-7ffac3560b20&groupId=13e42e94-3404-46d8-b605-228c35984df3";
                tu1(accessToken,embedUrl);
            }
        },
        error:  function (XMLHttpRequest, textStatus, errorThrown) {
                    // msg
                    var errorTips = XMLHttpRequest.responseJSON.msg;
                    if (errorTips=='Division by zero'){
                        $(".layer1 .content").html("数据错误");
                        $(".layer1").show();
                        console.log(errorTips);
                    } else if(errorTips=='Token has expired'){
                        $(".layer1 .content").html("令牌过期");
                        $(".layer1").show();
                        window.location.href="login.html";
                    }


                }

    });


    // 图1
    function tu1(accessToken,embedUrl) {
        accessToken = accessToken;
        embedUrl = embedUrl;
        console.log(embedUrl);
        var embedReportId = "af34d2e5-3f9d-4e88-ae1d-7ffac3560b20";
        var models = window['powerbi-client'].models;
        var config = {
            type: 'report',
            // tokenType: models.TokenType.Embed,
            accessToken: accessToken,
            embedUrl: embedUrl,
            id: embedReportId,
            viewMode: models.ViewMode.View,
            // filters: filter,
            permissions: models.Permissions.All ,
            settings: {
                filterPaneEnabled: false,
                navContentPaneEnabled: false
            }
        };
        // Get a reference to the embedded report HTML element
        var tu11 = $('.tu11')[0];
        if ("" != "") {
            $("#RLS").prop('checked', true);
            $("#RLSdiv").show();
        }
        else
        {
            $("#RLS").prop('checked', false);
            $("#RLSdiv").hide();
        }
        if ("False" == "True") {
            $("#noRLSdiv").hide();
            $("#RLS").removeAttr("disabled");
            $("#RLS").change(function () {
                if ($(this).is(":checked")) {
                    $("#RLSdiv").show(300);
                } else {
                    $("#RLSdiv").hide(200);
                }
            });
        }
        else
        {
            $("#noRLSdiv").show();
        }
        var report = powerbi.embed(tu11, config);
    };

    // 确定
    var pingjieIndex1 = 0;
    $(".application").on("click",function () {
        var detailsdata1 = $(".details1").val();
        var detailsdata2 = $(".details2").val();
        var detailsdata3 = $(".details3").val();
       // var commodity1Time2 = $("#usertype").val();
     
        // 拼接图形盒子1
        pingjieIndex1++;
        var tu1Id = "reportContainer1";
        tu1Id = tu1Id + pingjieIndex1;
        console.log(tu1Id);
        var arrtx1 = [];
        arrtx1.push("<div class='tu11 no-border'" + "id=" +tu1Id +">");
        arrtx1.push("</div>");
        str0 = arrtx1.join('');
        $(".tu1").append(str0);
        // 删除其他元素
        $('.tu1'+' #'+tu1Id).siblings().remove();
        var embedUrl = "https://app.powerbi.cn/reportEmbed?reportId=af34d2e5-3f9d-4e88-ae1d-7ffac3560b20&groupId=13e42e94-3404-46d8-b605-228c35984df3";
        embedUrl = `${embedUrl}&filter=platform/platform eq '${detailsdata1}' and store_name/store_name eq '${detailsdata2}' and year/year eq'${detailsdata3}'`
        $.ajax({
            url: pageUrl+'/api/common/getaccesstoken',
            type: 'POST',
            dataType: 'json',
            success: function (data) {
                var msg = '';
                if (data) {
                    var accessToken = data.data.accessToken.value;
                    tu1(accessToken,embedUrl);
                }
            },
            error:  function (XMLHttpRequest, textStatus, errorThrown) {
                // msg
                var errorTips = XMLHttpRequest.responseJSON.msg;
                if (errorTips=='Division by zero'){
                    $(".layer1 .content").html("数据错误");
                    $(".layer1").show();
                    console.log(errorTips);
                } else if(errorTips=='Token has expired'){
                    $(".layer1 .content").html("令牌过期");
                    $(".layer1").show();
                    window.location.href="login.html";
                }


            }

        });


    });



});